package edu.sctu.practice.class02.hujie.my_practice.day20151112;

/**
 * Created by user14 on 2015/11/12.
 */
public class tryCatch {
    public static void main(String[] args) {
        tryCatch tct = new tryCatch();
        int result1 = tct.test1();//创建变量接收返回值
        System.out.println("test1()方法执行了 返回值是：" + result1);
        int result2 = tct.test2();
        System.out.println("我想大声告诉你test2()执行完毕！！~");
        int result3 = tct.test3();
        System.out.println("我想大声告诉你test3()执行完毕！！~");
    }

    /**
     * divider(除数)
     * result(结果)
     * try-catch捕获while循环
     * 每次循环，divider减一，result=result+100/divider
     * 如果：捕获异常，打印输出“抛出异常了”，返回-1
     * 否则：返回result
     */
    public int test1() {
        int divider = 10;
        int result = 100;
        try {
            while (divider > -1) {
                divider--;
                result = result + 100 / divider;
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("循环抛出异常了！！");
            return -1;
        }
    }

    public int test2() {
        int divider = 10;
        int result = 100;
        try {
            while (divider > -1) {
                divider--;
                result = result + 100 / divider;
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("循环抛出异常了！！");
            return result = 999;
        } finally {
            System.out.println("这是finally!!哈哈哈");
            System.out.println("我是result!!我的值是：" + result);
        }
    }

    public int test3() {
        int divider = 10;
        int result = 100;
        try {
            while (divider > -1) {
                divider--;
                result = result + 100 / divider;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("循环抛出异常了！！");
        } finally {
            System.out.println("这是finally!!哈哈哈");
            System.out.println("我是result!!我的值是：" + result);
        }
        System.out.println("我是test3()，我执行了");
        return 100;
    }
}
